/*-//GEN-BEGIN:copyright
 * ForBILE - Framework for Building Interactive Learning Environments
 * Computing Institute - Federal University of Alagoas - Brazil
 * Group of Otimization of the Web
 *
 * Copyright (C) 2007 Ig Bittencourt (ig.ibert@gmail.com)
 *		      Camila Nunes (camilapbnunes@hotmail.com)
 *	              Marcos Tadeu (zero.ufal@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *///GEN-END:copyright
package br.ufal.ic.forbile.agents.behaviours;

import br.ufal.ic.forbile.agents.ForBILEAgent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import java.util.logging.Logger;

/**
 * GeneralSensor1.java
 *
 * <p>A full description of this class.
 *
 * @see SomeRelatedClass.
 *
 * @author <a href="mailto:ig.ibert@gmail.com">ig</a>.
 * @author <a href="mailto:camilapbnunes@hotmail.com">camila</a>.
 * @since 0.1
 * @version 0.1
 *
 */
public abstract class GeneralSensor extends TickerBehaviour {
    
    // Constants -----------------------------------------------------
    
    // Attributes ----------------------------------------------------
    
    private ForBILEAgent agent;
    
    // Static --------------------------------------------------------
    
    private static Logger logger;
    
    static {
        logger = Logger.getLogger(GeneralSensor.class.getName());
    }
    
    // Constructors --------------------------------------------------
    
    /**
     * Creates a new instance of GeneralSensor1
     */
    public GeneralSensor(final ForBILEAgent agent, final long time) {
        super(agent, time);
        this.agent = agent;
    }
    
    // Public --------------------------------------------------------
    
    
    /**
     * Gets the ForBILEAgent agent.
     *
     * @return The agent.
     */
    public ForBILEAgent getAgent() {
        return agent;
    }
    
    /**
     * Calls the specific behaviour according to the service.
     *
     * @param message The message will be called.
     */
    protected abstract void callBehaviour(final ACLMessage message);
    
    // TickerBehaviour implementation -----------------------------
    
    protected void onTick() {
        ACLMessage message = this.myAgent.receive();
        logger.info("------- " + this.myAgent.getLocalName() +  " percepting the environment -------");
        if (message != null) {
            logger.info("------- Message intercepted by " +  this.myAgent.getLocalName() + "-------");
            if ( (message.getInReplyTo() != null) && (message.getSender().getLocalName().equals("PersistenceOWLAgentInteraction")) ) {
                if (message.getInReplyTo().equals("RETRIEVE")) {
                    this.agent.registerServices();
                } else {
                    this.callBehaviour(message);
                }
                
            } else {
                this.callBehaviour(message);
            }
        }
    }
    
    // Y overrides ---------------------------------------------------
    
    // Package protected ---------------------------------------------
    
    // Protected -----------------------------------------------------
    
    // Private -------------------------------------------------------
    
    // Inner classes -------------------------------------------------
    
}